package cn.com.klun.tjy.other.ThreadsTest;

import org.apache.poi.ss.formula.functions.T;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * 头条一则面试题
 * 题目：启动两个线程向文件temp.txt中写入递增的数字
 * 线程1先写入 thread-1，thread-2，thread-3，thread-4，thread-5，
 * 然后是线程2 再写入thread-6，thread-7，thread-8，thread-9，thread-10，
 * 接着再由线程1写入thread-11，thread-12，thread-13，thread-14，thread-15
 * 以此类推，知道打印到50，程序为
 */
public class Ttmst {

    private volatile static int i = 1;

    private static void print(String threadName){
        System.out.println(threadName+"-"+i++);
        if(i>=50){
            System.out.println("结束打印");
        }
    }
    private static void demo1(){
        Object lockA = new Object();
        Object lockB = new Object();
        Thread A =  new Thread(new Runnable() {
            @Override
            public void run() {
                boolean flag = true;
                synchronized (lockA){
                    while(flag){
                        print("ThreadA");
                        if(i%5 == 0) {
                            try {
                                lockB.notify();
                                lockA.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        if(i>=50){
                            flag = false;
                        }
                    }
                }
            }
        });
        Thread B = new Thread(new Runnable() {
            @Override
            public void run() {
                boolean flag = true;
                synchronized (lockB){
                    while(flag){
                        print("ThreadB");
                        if(i%5 == 0) {
                            try {
                                lockA.notify();
                                lockB.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        if(i>=50){
                            flag = false;
                        }
                    }
                }
            }
        });
        A.start();
        B.start();
    }
    private static void printNumber(String a){
        int i = 0;
        while(i++ < 3){
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(a+" print:"+i);
        }
    }

    private static void runDAfterABC(){
        int worker = 3;
        CountDownLatch countDownLatch = new CountDownLatch(worker);
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("D is waiting for other three threads");
                try {
                    countDownLatch.await();
                    System.out.println("All done , D starts working");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        // 三个线程同步打印
        for (char threadName='A'; threadName <='C';threadName++){
            final String tN = String.valueOf(threadName);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(tN + "is working");
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(tN + "finished");
                    countDownLatch.countDown();
                }
            }).start();

        }
    }
    //三个线程各自准备，等三个线程都发出准备好的信号，一期跑 CyclicBarrier
    private static void runABCWhenAllReady(){
        int runner = 3;
        CyclicBarrier cyclicBarrier = new CyclicBarrier(runner);
        final Random random = new Random();
        for(char runnerName='A';runnerName <='C';runnerName++){
            final String rN =  String.valueOf(runnerName);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    long prepareTime  = random.nextInt(10000)+100;
                    System.out.println(rN + " is preparing for time:"+prepareTime);
                    try {
                        Thread.sleep(prepareTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        System.out.println(rN+"准备完毕");
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println(rN+" all start");
                }
            }).start();
        }
    }

    public static void main(String[] args) {

        Worker a = new Worker();
        a.setName("ThreadA-");
        Worker b = new Worker();
        b.setName("ThreadB-");

        a.start();
        b.start();

    }
}
class Worker extends Thread{
    @Override
    public void run() {
        while(true){
            calPrint(this.getName(),1);
        }
    }
    public synchronized void calPrint(String name,int i){
//        for (int i = 0;i <= 50; i++) {
//            try {
//                Thread.sleep(100);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
            System.out.println(name+"-"+i);
//        }
    }

}





